import json
from time import sleep

import requests

from requests.cookies import RequestsCookieJar
import login_info.login_info as login
import tools.log_op as login_op
from tools.login_op import get_login_cookie
import tools.make_string as name
class river_api1():
    def __init__(self):
        self.emr_user = login.emr_user
        self.emr_password = login.emr_password
        self.get_cookie_url = login.river_get_cookie_url
        self.cookies = self.get_cookies()


    def get_cookies(self):
        # get_cookie_url,emr_user,emr_password,flag='studio'
        login_dict = json.loads(login_op.get_head())
        head = login.win_2
        if (login_dict !={}):
            head["X-XSRF-TOKEN"] = login_dict['xsr']
            head["Cookie"] = login_dict['head']
        else:
            a = get_login_cookie(self.get_cookie_url,self.emr_user,self.emr_password,flag='river')
            head["X-XSRF-TOKEN"] = a.xsr
            head["Cookie"] = a.head_cookie
        self.head = head

    def get_table_desc(self,table_name):
        result = requests.post(url="http://10.45.14.10:51001/metadataManage/api/restful/v1/dataView/tables",
                               headers=self.head, data=json.dumps({"keyword": f'{table_name}', "startIndex": "1", "pageSize": "30","timestamp": 1588915705501, "sourceType": "ALL_TABLES"}))
        table_result = result.json()
        if(table_result['code']==20000):
            tablelist = table_result['data']['tables']
            for list in tablelist:
                if(list['name'] == f'{table_name}'):
                    sourceTableGuid = list['guid']
                    dataSourceGuid = list['dataSourceGuid']
                    table_dict = json.loads(login_op.get_table_desc())
                    table_dict[f'{table_name}']['sourceTableGuid'] = sourceTableGuid
                    table_dict[f'{table_name}']['dataSourceGuid'] = dataSourceGuid
                    login_op.put_json(table_dict)
                    # 创建任务用sourceTableGuid
                    table_std = json.loads(login_op.get_stand_desc())
                    table_std[f'{table_name}']['sourceTableGuid'] = sourceTableGuid
                    table_std[f'{table_name}']['dataSourceGuid'] = dataSourceGuid

                    login_op.put_desc(table_std)
        else:
            print(json.dump(table_result))
            login_op.wrong_table(json.dump(table_result))
    def copytable(self,table_desc,flag='std'):
        self.tb_name=''
        key = list(table_desc.keys())[0]
        if(flag=='std'):
            self.tb_name = name.std_name(key)
        else:
            self.tb_name = name.err_name(key)
        columns =table_desc[key]["columns"]
        sourceTableGuid = table_desc[key]["sourceTableGuid"]
        prams = login.clone_table
        prams['tableName'] = self.tb_name
        prams['columns'] = columns
        prams['sourceTableGuid'] = sourceTableGuid
        result = requests.post(url="http://10.45.14.10:51001/metadataManage/api/restful/v1/tables?mode=form",headers=self.head, data=json.dumps(prams)).json()
        a = result['message']
        if(a=='success'):
            print('表创建成功',self.tb_name)
        elif(a == '项目空间的表已存在'):
            print('项目空间的表已存在',self.tb_name)
        else:
            login_op.wrong_table(str(result))
            print(str(result))


    # 批量表结构
    def batch_get_table_desc(self):
        mission = login_op.get_mission_from_empty()
        mission = [i for i in mission if (len(str(i)) != 0)]
        for x in mission:
            self.get_table_desc(x)
    # 批量清洗表
    def batch_copy(self):
        table_dict = json.loads(login_op.get_table_desc())
        for x in list(table_dict.keys()):
            table_desc = {x:table_dict[x]}
            self.copytable(table_desc)
            self.copytable(table_desc,'err')
    # 批量jobTojson
    def batch_job(self):
        stand_dict = json.loads(login_op.get_stand_desc())
        for x in list(stand_dict.keys()):
            table_desc = {x:stand_dict[x]}
            self.job_to_json(table_desc)
            sleep(3)
    #jobTojson
    def job_to_json(self,std_json):
        params = login.job_to_json
        ods_name = list(std_json.keys())[0]
        job_name = name.node_name(ods_name)
        std_name = name.std_name(ods_name)
        err_name = name.err_name(ods_name)
        send_list = std_json[list(std_json.keys())[0]]["columns"]
        var = send_list[0]["column_name"]
        var_type = send_list[0]["column_type"]
        params['job_name'] = job_name
        params['destination_table'] = std_name
        params["exception_table"] =err_name
        params['source_table_guid'] = std_json[list(std_json.keys())[0]]['sourceTableGuid']
        params["source_table_name"] = ods_name
        params["duplicate_config"]["duplicate_key"] = [var]
        params["standard_config"] = send_list
        params["base_config"] = [{"column_name":var,"column_type":var_type,"null_check_filter":{
                "status":"true"},"isNull":"true","expressionWord":"",}]
        # first_url = 'http://10.45.14.10:51001/dq/api/restful/v1/jobTempleteConfig/'+std_json[list(std_json.keys())[0]]['sourceTableGuid']
        # result = requests.post(url=first_url,headers=self.head,data=json.dumps(params)).json()
        # if(result['code']==0):
        #     result1 = requests.get(url=first_url,headers=self.head).json()
        #     if(result1['code']!=0):
        #         print('get出错'+str(result1))
        #         login_op.job_err('get出错'+str(result1))
        #         return
        # else:
        #     print('第一次post出错'+str(result))
        #     login_op.job_err('第一次post出错'+str(result))
        #     login_op.wirte_empty_fail(ods_name)
        #     return
        result = requests.post(url="http://10.45.14.10:51001/dq/api/restful/v1/jobTemplete/jobToJson",headers=self.head, data=json.dumps(params)).json()
        work_send =''
        if(result['code']==20000):
            work_send = result['data']['job']
            work_send["nodeId"] = "40b0215a-5a2e-4032-a477-dea4414b37ec"
            work_send["rootId"] = "40b0215a-5a2e-4032-a477-dea4414b37ec"
            result = requests.post(url="http://10.45.14.10:51001/dq/api/restful/v1/job",headers=self.head, data=json.dumps(work_send)).json()
            print("win"+ods_name)
            login_op.wirte_empty_success(ods_name)

        else:
            login_op.job_err(str(result))
            print(str(result))
        print('api113')
if __name__ == '__main__':

    a = river_api1()
    a.batch_get_table_desc()
    sleep(3)
    a.batch_copy()
    # sleep(4)
    # a.job_to_json({"ods_cz_ykcw_fa_barn_sort": {"columns": [{"column_name": "barn_sort_code", "isCleanRule": "false", "nameEntity": [], "expressionWord": "", "column_type": "string", "keep_mode": "NULL"}, {"column_name": "barn_sort_name", "isCleanRule": "false", "nameEntity": [], "expressionWord": "", "column_type": "string", "keep_mode": "NULL"}, {"column_name": "barn_pre_code", "isCleanRule": "false", "nameEntity": [], "expressionWord": "", "column_type": "string", "keep_mode": "NULL"}, {"column_name": "barn_sort_purp", "isCleanRule": "false", "nameEntity": [], "expressionWord": "", "column_type": "string", "keep_mode": "NULL"}, {"column_name": "remark", "isCleanRule": "false", "nameEntity": [], "expressionWord": "", "column_type": "string", "keep_mode": "NULL"}, {"column_name": "barn_inex_sort", "isCleanRule": "false", "nameEntity": [], "expressionWord": "", "column_type": "string", "keep_mode": "NULL"}], "sourceTableGuid": "fd4e98a5-0f38-412a-9202-5a89fa73ede9", "dataSourceGuid": "49f808e4-cf1b-4f19-b808-bf463e6aa307"}})
    a.batch_job()